<?php

$smarty->configLoad($lang . '/options.module.php');
$language = parse_ini_file($lang . '/breadcrumb.module.php', true);
$breadcrumb = $language['options'];

if (isset($_POST['submit'])) {
	$main->logAction($_SESSION['id'], 'module_install');
	if (!file_exists('module_install')) {
		mkdir('module_install');
	}
	move_uploaded_file($_FILES['module']['tmp_name'], "module_install/module.zip");

if (function_exists('zip_open'))
	{
		$zip_datei = 'module_install/module.zip';

		$ziel_ordner = 'module_install/';

		if (file_exists($zip_datei) && ($zip = zip_open($zip_datei)))
		{
			while($zip_entry = zip_read($zip))
			{
				$file_name = zip_entry_name($zip_entry);
				$file_size = zip_entry_filesize($zip_entry);
				$comp_meth = zip_entry_compressionmethod($zip_entry);

				if (zip_entry_open($zip, $zip_entry, 'rb'))
				{
					$buffer = zip_entry_read($zip_entry, $file_size);

					if (preg_match('/\/$/', $file_name) && ($comp_meth == 'stored'))
					{
						if (!is_dir($ziel_ordner . $file_name))
							@mkdir($ziel_ordner . $file_name, 0777);
					}
					else
					{
						$fp = fopen($ziel_ordner . $file_name, 'wb');
						fwrite($fp, $buffer);
						fclose($fp);
					}

					zip_entry_close($zip_entry);
				}
			}

			zip_close($zip);
		}
		else
			echo 'Cant find module.zip';
	}
	else {
		echo   'Please Activate php_zip.dll in your PHP.ini File';
	}

	function rrmdir($dir) {
		foreach (glob($dir . '/*') as $file) {
			if (is_dir($file))
				rrmdir($file);
			else
				unlink($file);
		}
		rmdir($dir);
	}

	function full_copy($source, $target) {
		if (is_dir($source)) {
			@mkdir($target);

			$d = dir($source);

			while (FALSE !== ( $entry = $d->read() )) {
				if ($entry == '.' || $entry == '..') {
					continue;
				}

				$Entry = $source . '/' . $entry;
				if (is_dir($Entry)) {
					full_copy($Entry, $target . '/' . $entry);
					continue;
				}
				copy($Entry, $target . '/' . $entry);
			}

			$d->close();
		} else {
			copy($source, $target);
		}
	}
	if(file_exists("module_install/module/")) {
	full_copy("module_install/module/", "./");
	}
	if(file_exists("module_install/module/sql/module.sql")) {
		$import = file_get_contents("module_install/module/sql/module.sql");
		$import = preg_replace("%/\*(.*)\*/%Us", '', $import);
		$import = preg_replace("%^--(.*)\n%mU", '', $import);
		$import = preg_replace("%^$\n%mU", '', $import);

		mysql_real_escape_string($import);
		$import = explode(";", $import);

		foreach ($import as $imp) {
			if ($imp != '' && $imp != ' ') {
				$connection->exec($imp);
			}
		}
	}
	rrmdir('module_install');
	rrmdir('sql');
	$main->jqmodal('installed');
    echo '<meta http-equiv="refresh" content="5; URL=index.php?mod=options&page=modules">';

}


$template = 'options/modules_install.tpl';
?>
 